**************************
***	RUN USING STATA 16 ***
**************************

* Purpose: IRT analysis for all Maths objective items. Outputs parameters.
* Last Updated: 06 May 2021

*----------------------------------------------------------------------------------------------
* Part 1 MATHS multiple-choice IRT
*----------------------------------------------------------------------------------------------

	use "$output\mat_unique.dta", clear	
	
	//first, drop observations for which we only have constructed responses
		drop if set==1
	
	//begin by putting items in sequential order
		order *, seq
		order pid, first
		keep pid booklet o_*
		
	// drop selected items (26 in total)
	*1) 9 items have only missing values (those items with error which were not scored)
		drop o_149 o_712 o_626 o_817 o_918 o_721 o_404 o_405 o_932		
	*2) 4 items failed to generate starting values (a<0)
		drop o_347 o_806 o_340 o_749
	*3) 13 items are unstable during estimation. Drop these
		drop o_234 o_718 o_114 o_242 o_842 o_150 o_819 o_225 o_942 o_527 o_717 o_826 o_314
		
	cd "$graph\icc\mat" //set icc directory
	uirt o_*, icc icc_format(gph) icc_bins(40) //run irt, output icc for each item
	
	//extract all item parameters
		matrix c = e(item_par) 
		svmat c
		matrix se = e(item_par_se) 
		svmat se
		
	//add names of variables alongside parameters
	gen item = ""
	local y=0
	foreach var of varlist o_* {
		local y = `y' + 1
		replace item = "`var'" in `y'
	}
	
		ren c1 disc
		ren c2 diff
		ren se1 se_disc
		ren se2 se_diff
		keep disc-item
		
	gen x=_n
	drop if disc==.
 
	//save item parameters
	save "$output\mat_obj_par.dta", replace

